package com.xunji.standard.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xunji.standard.client.XunjiUserClient;
import com.xunji.standard.common.R;
import com.xunji.standard.entity.CaseShare;
import com.xunji.standard.mapper.CaseShareMapper;
import com.xunji.standard.service.CaseShareService;
import com.xunji.standard.vo.rep.CaseShareUserResponse;
import com.xunji.standard.vo.req.CaseShareRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: xuqiang_imyours
 * @Date: 2025/7/2 16:31
 * @Description:
 **/
@Service
public class CaseShareServiceImpl extends ServiceImpl<CaseShareMapper, CaseShare> implements CaseShareService {

    @Resource
    XunjiUserClient xunjiUserClient;

    @Override
    public void add(CaseShareRequest request, String userId) {
        R r = xunjiUserClient.getUserName(userId);
        String userName = (String) r.getData();

        String[] beUserIds = request.getBeUserIds();
        for (String beUserId : beUserIds) {
            R r1 = xunjiUserClient.getUserName(beUserId);
            String beUserName = (String) r1.getData();

            CaseShare caseShare = new CaseShare();
            caseShare.setCaseId(request.getCaseId());
            caseShare.setUserId(userId);
            caseShare.setUserName(userName);
            caseShare.setBeUserId(beUserId);
            caseShare.setBeUserName(beUserName);
            this.save(caseShare);
        }
    }

    @Override
    public ArrayList<CaseShareUserResponse> getListByUserId(String userId, String caseId) {
        QueryWrapper<CaseShare> qw = new QueryWrapper<>();
        qw.eq("user_id", userId).eq("case_id", caseId);
        List<CaseShare> caseShares = this.baseMapper.selectList(qw);
        ArrayList<CaseShareUserResponse> responses = new ArrayList<>();
        if (caseShares != null && !caseShares.isEmpty()) {
            for (CaseShare caseShare : caseShares) {
                CaseShareUserResponse response = new CaseShareUserResponse(caseShare.getId(), caseShare.getBeUserId(), caseShare.getBeUserName());
                responses.add(response);
            }
        }
        return responses;
    }

    @Override
    public void deleteById(int id) {
        this.baseMapper.deleteById(id);
    }

    @Override
    public List<String> selectCaseListByUserId(String beUserId) {
        QueryWrapper<CaseShare> qw = new QueryWrapper<>();
        qw.eq("be_user_id", beUserId);
        List<CaseShare> caseShares = this.baseMapper.selectList(qw);
        ArrayList<String> caseIds = new ArrayList<>();
        if (caseShares != null && !caseShares.isEmpty()) {
            for (CaseShare caseShare : caseShares) {
                caseIds.add(caseShare.getCaseId());
            }
        }
        return caseIds;
    }

    @Override
    public void deleteByCaseId(String caseId) {
        QueryWrapper<CaseShare> qw = new QueryWrapper<>();
        qw.eq("case_id", caseId);
        this.baseMapper.delete(qw);
    }

    @Override
    public void deleteByCaseIdAndUserId(String caseId, String userId) {
        QueryWrapper<CaseShare> qw = new QueryWrapper<>();
        qw.eq("case_id", caseId).eq("be_user_id", userId);
        this.baseMapper.delete(qw);
    }
}
